模块:Infobox
-- This module implements -- 中文的此模块对比英文维基多解析了header/labal/data*style参数。 -- local p = {} local navbar = require('Module:Navbar')._navbar local args = {} local origArgs local root local function union(t1, t2) -- Returns the union of the values of two tables, as a sequence. local vals = {} for k, v in pairs(t1) do valsv = true end for k, v in pairs(t2) do valsv = true end local ret = {} for k, v in pairs(vals) do table.insert(ret, k) end return ret end local function getArgNums(prefix) -- Returns a table containing the numbers of the arguments that exist -- for the specified prefix. For example, if the prefix was 'data', and -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}. local nums = {} for k, v in pairs(args) do local num = tostring(k):match('^' .. prefix .. '(1-9%d*)$') if num then table.insert(nums, tonumber(num)) end end table.sort(nums) return nums end local currentsection; local function addRow(rowArgs) -- Adds a row to the infobox, with either a header cell -- or a label/data cell combination. if rowArgs.header then currentsection = root:tag('div') currentsection:addClass(rowArgs.rowclass) currentsection:addClass('pi-item') currentsection:addClass('pi-group') currentsection:addClass('pi-border-color') currentsection:cssText(rowArgs.rowstyle) currentsection:attr('id', rowArgs.rowid) currentsection :tag('div') :attr('id', rowArgs.headerid) :addClass(rowArgs.class) :addClass('pi-item') :addClass('pi-header') :addClass('pi-secondary-font') :addClass('pi-item-spacing') :addClass('pi-secondary-background') :css('text-align', 'left') :css('background', args.bgcolor2) :cssText(rowArgs.headerstyle) :wikitext(rowArgs.header) elseif rowArgs.data then local row if currentsection then row = currentsection:tag('div') else row = root:tag('div') end row:addClass(rowArgs.rowclass) row:addClass('pi-item') row:addClass('pi-item-spacing') row:addClass('pi-border-color') row:addClass('pi-data') row:cssText(rowArgs.rowstyle) row:attr('id', rowArgs.rowid) if rowArgs.label then row :tag('div') :addClass('pi-data-label') :addClass('pi-secondary-font') :css('text-align', 'left') :attr('id', rowArgs.labelid) :cssText(rowArgs.labelstyle) :wikitext(rowArgs.label) :done() end local dataCell = row:tag('div') dataCell :attr('id', rowArgs.dataid) :addClass(rowArgs.class) :addClass('pi-data-value') :addClass('pi-font') :cssText(rowArgs.datastyle) :newline() :wikitext(rowArgs.data) end end local function addFigure(figureArgs) -- Adds a row to the infobox, with either a header cell -- or a label/data cell combination. root :tag('div') :addClass(figureArgs.class) :addClass('pi-item') :addClass('pi-image') :attr('data-source', figureArgs.dataname) :cssText(figureArgs.datastyle) :wikitext(figureArgs.data) end local function renderTitle() if not args.title then return end root :tag('div') :addClass('pi-item') :addClass('pi-item-spacing') :addClass('pi-title') :css('text-align', 'center') :css('background', args.bgcolor1) :css('color', args.titlecolor or 'white') :css('padding', '0.25em') :css('font-size', '1.3em') :css('line-height', '1.6') :css('font-weight', 'bold') :wikitext(args.title) end local function renderImages() if args.image then args.image1 = args.image end local style = 'width:250px; background:' .. args.bgcolor1 if args.imagestyle then style = args.imagestyle end local imagenums = getArgNums('image') for k, num in ipairs(imagenums) do local data = mw.html.create():wikitext(args.. tostring(num)) addFigure({ data = tostring(data), datastyle = style, class = args.imageclass, dataname = 'image' .. tostring(num) }) end end local function renderRows() -- Gets the union of the header and data argument numbers, -- and renders them all in order using addRow. local rownums = union(getArgNums('header'), getArgNums('data')) table.sort(rownums) for k, num in ipairs(rownums) do addRow({ header = args.. tostring(num), headerstyle = (args.headerstyle or '') .. (args.headerstyle and ';' or '') .. (args.. tostring(num) .. 'style' or ''), label = args.. tostring(num), labelstyle = (args.labelstyle or '') .. (args.labelstyle and ';' or '') .. (args.. tostring(num) .. 'style' or ''), data = args.. tostring(num), datastyle = (args.datastyle or '') .. (args.datastyle and ';' or '') .. (args.. tostring(num) .. 'style' or ''), class = args.. tostring(num), rowclass = args.. tostring(num), rowstyle = args.. tostring(num), dataid = args.. tostring(num), labelid = args.. tostring(num), headerid = args.. tostring(num), rowid = args.. tostring(num) }) end end local function renderNavBar() if not args.name then return end root :tag('div') :addClass('pi-navigation') :addClass('pi-item-spacing') :addClass('pi-secondary-background') :addClass('pi-secondary-font') :css('text-align', 'center') :css('background', args.bgcolor1) :css('color', args.titlecolor or 'white') :css('font-size', '0.8em') :wikitext('[.. args.name .. '|' .. mw.getCurrentFrame():expandTemplate({ title = 'color', args = { args.titlecolor or 'white', '[查看模板' } }) .. ']]') end local function renderItalicTitle() local italicTitle = argstitle' and mw.ustring.lower(argstitle') if italicTitle '' or italicTitle 'force' or italicTitle 'yes' then root:wikitext(mw.getCurrentFrame():expandTemplate({title = 'italic title'})) end end local function renderTrackingCategories() if args.decat ~= 'yes' then if #(getArgNums('data')) 0 and mw.title.getCurrentTitle().namespace 0 then root:wikitext('Category:使用无数据行信息框模板的条目') end if args.child 'yes' and args.title then root:wikitext('Category:使用带有标题参数的嵌入式信息框模板的条目') end end end local function _infobox() -- Specify the overall layout of the infobox, with special settings -- if the infobox is used as a 'child' inside another infobox. local ischild = false if args.child ~= 'yes' then root = mw.html.create('div') root :addClass('portable-infobox') :addClass('pi-background') :addClass('pi-theme-wizard') :addClass('pi-layout-default') :addClass(args.bodyclass) :css('float', 'right') :css('clear', 'right') :css('width', '250px') :css('text-align', 'left') :css('font-size', '0.8em') :css('line-height', '1.6em') :css('border-collapse', 'collapse') :cssText(args.bodystyle) else ischild = true root = mw.html.create() root :wikitext(args.title) end renderImages() renderTitle() renderRows() renderNavBar() --renderItalicTitle() renderTrackingCategories() return tostring(root) end local function preprocessSingleArg(argName) -- If the argument exists and isn't blank, add it to the argument table. -- Blank arguments are treated as nil to match the behaviour of ParserFunctions. if origArgsargName and origArgsargName ~= '' then argsargName = origArgsargName end end local function preprocessArgs(prefixTable, step) -- Assign the parameters with the given prefixes to the args table, in order, in batches -- of the step size specified. This is to prevent references etc. from appearing in the -- wrong order. The prefixTable should be an array containing tables, each of which has -- two possible fields, a "prefix" string and a "depend" table. The function always parses -- parameters containing the "prefix" string, but only parses parameters in the "depend" -- table if the prefix parameter is present and non-blank. if type(prefixTable) ~= 'table' then error("Non-table value detected for the prefix table", 2) end if type(step) ~= 'number' then error("Invalid step value detected", 2) end -- Get arguments without a number suffix, and check for bad input. for i,v in ipairs(prefixTable) do if type(v) ~= 'table' or type(v.prefix) ~= "string" or (v.depend and type(v.depend) ~= 'table') then error('Invalid input detected to preprocessArgs prefix table', 2) end preprocessSingleArg(v.prefix) -- Only parse the depend parameter if the prefix parameter is present and not blank. if argsv.prefix and v.depend then for j, dependValue in ipairs(v.depend) do if type(dependValue) ~= 'string' then error('Invalid "depend" parameter value detected in preprocessArgs') end preprocessSingleArg(dependValue) end end end -- Get arguments with number suffixes. local a = 1 -- Counter variable. local moreArgumentsExist = true while moreArgumentsExist true do moreArgumentsExist = false for i = a, a + step - 1 do for j,v in ipairs(prefixTable) do local prefixArgName = v.prefix .. tostring(i) if origArgsprefixArgName then moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones. preprocessSingleArg(prefixArgName) end -- Process the depend table if the prefix argument is present and not blank, or -- we are processing "prefix1" and "prefix" is present and not blank, and -- if the depend table is present. if v.depend and (argsprefixArgName or (i 1 and argsv.prefix)) then for j,dependValue in ipairs(v.depend) do local dependArgName = dependValue .. tostring(i) preprocessSingleArg(dependArgName) end end end end a = a + step end end function preprocessSpecificStyle(styleTable, step) -- Assign the parameters *style to the args table local a = 1 -- Counter variable. local moreArgumentsExist = true while moreArgumentsExist true do moreArgumentsExist = false for i = a,a + step - 1 do for j,v in ipairs(styleTable) do local styleArgName = v.arg .. tostring(i) .. 'style' if origArgsstyleArgName then moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones. preprocessSingleArg(styleArgName) end end end a = a + step end end function p.infobox(frame) -- If called via #invoke, use the args passed into the invoking template. -- Otherwise, for testing purposes, assume args are being passed directly in. if frame mw.getCurrentFrame() then origArgs = frame:getParent().args else origArgs = frame end -- Parse the data parameters in the same order that the old did, so that -- references etc. will display in the expected places. Parameters that depend on -- another parameter are only processed if that parameter is present, to avoid -- phantom references appearing in article reference lists. preprocessSingleArg('child') preprocessSingleArg('bodyclass') preprocessSingleArg('bodystyle') preprocessSingleArg('title') preprocessSingleArg('titlecolor') preprocessArgs({ {prefix = 'image'} }, 10) preprocessSingleArg('imagestyle') preprocessSingleArg('imageclass') preprocessSingleArg('bgcolor1') preprocessSingleArg('bgcolor2') preprocessArgs({ {prefix = 'header'}, {prefix = 'data', depend = {'label'}}, {prefix = 'rowclass'}, {prefix = 'rowstyle'}, {prefix = 'class'}, {prefix = 'dataid'}, {prefix = 'labelid'}, {prefix = 'headerid'}, {prefix = 'rowid'} }, 80) preprocessSpecificStyle({ {arg = 'header'}, {arg = 'label'}, {arg = 'data'} }, 80) preprocessSingleArg('headerstyle') preprocessSingleArg('labelstyle') preprocessSingleArg('datastyle') preprocessSingleArg('name') argstitle' = origArgstitle' -- different behaviour if blank or absent preprocessSingleArg('decat') return _infobox() end return p